import cv2 as cv
import socket
import struct

HOST = '219.228.135.234'
PORT = 7890

server = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
server.connect((HOST, PORT))
print('now start to send..')
capture = cv.VideoCapture(0)
print('camera opened..')
try:
    while True:
        success, frame = capture.read()
        while not success and frame is None:
            success, frame = capture.read()
        result, img_encode = cv.imencode('.jpg', frame, [cv.IMWRITE_JPEG_QUALITY, 50])
        server.sendall(struct.pack('i', img_encode.shape[0]))
        server.sendall(img_encode)
except Exception as e:
    print(e)
    server.sendall(struct.pack('c', 1))
    capture.release()
    server.close()

    